Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education
🎓 100% Free · Hindi me · Beginner se Career tak

Python Tutorial in HindiiLearn Python Programming for Free

Python, AI, Data Analytics aur SQL ek hi jagah, simple Hindi me. Padho, browser me code chalao, projects banao aur career shuru karo.

main.py
# Python seekho, Hindi me naam = "Ravi" for i in range(3): print("Namaste", naam)
▸ python main.py
🐍 Python 🤖 AI & ML 📊 Data 🗄️ SQL

Chahe Student ho ya Professional, yahan aapke liye sab hai

Apna card chuno, seedha sahi jagah pahunch jaoge.

🎓

Student ho?

School ya college me ho? Python zero se seekho, notes aur cheat sheets ke saath.

  • Python basics se OOP tak
  • Notes, PDFs aur Cheat Sheets
  • Browser me practice, kuch install nahi
💼

Working Professional ho?

Kaam ke saath nayi skill jodo: data, SQL aur AI se apna kaam tez karo.

  • Pandas, NumPy, Excel, Power BI
  • SQL aur MySQL databases
  • Automation aur AI Tools
🚀

Job ya Career change?

Projects banao, interview ki taiyari karo aur sahi roadmap follow karo.

  • Real projects, portfolio ke liye
  • Python aur SQL interview questions
  • AI Engineer, Data Scientist roadmap

Python Hindii Kaise Kaam Karta Hai?

Ek hi jagah 4 layers: seekho, practice karo, skills banao aur career shuru karo. Har layer ek step hai, us par click karke seedha wahan pahuncho. Computer par drag karke 3D me ghumao bhi.

Layer 1 · Seekho

Python Basics se Advanced tak

Variables, loops, functions se OOP tak, sab simple Hindi me. Yahin se shuru karo.

Naye ho? 3 step me shuru karo

💻 Online Code Editor

Browser me hi code likho aur turant chalao

Koi software install nahi. Code likho, Run dabao aur result usi waqt dekho.

  • Python, Java, C, C++, JavaScript, HTML aur aur bhi languages
  • input(), graphs aur multi-file HTML project bhi chalte hain
  • Mobile aur computer, dono par chalta hai
main.py
naam = input("Naam batao: ") for i in range(3): print("Namaste", naam)
▸ python main.py
PythonJavaCC++JavaScriptHTML

Python se AI tak ka poora rasta

8 steps, ek ke baad ek. Aage-peeche ghumao aur apni stage se seedha shuru karo.

Step 1 🐍

Python Basics

IntroductionVariablesData TypesOperatorsInput & OutputConditionsLoops
Start Python →
Step 2 💻

Python Intermediate

ListsTuplesSetsDictionariesStringsModulesFile Handling
Continue Python →
Step 4 🗄️

SQL & Databases

SQL BasicsMySQLSELECTINSERTUPDATEDELETEJOIN
Learn SQL →
Step 5 📊

Data Analytics

NumPyPandasData CleaningData VisualizationMatplotlibExploratory Data Analysis
Learn Data Analytics →
Step 6 🤖

Machine Learning

ML BasicsSupervised & Unsupervised LearningRegressionClassificationModel EvaluationScikit-learn
Learn Machine Learning →
Step 7 🧠

Artificial Intelligence

AI FundamentalsGenerative AIAI ToolsPrompt EngineeringLLM ConceptsAI Automation
Explore AI →
Step 8 🚀

Projects & Career

Python ProjectsAI ProjectsData ProjectsPortfolioGitHubInterview PreparationCareer Roadmap
Build Your Career →
Step 1 / 8

Python Directory in Hindi: mkdir, listdir, scandir सीखें

Python Directory क्या है? Folders बनाना, देखना, Rename और Delete करना

Python में files के साथ काम करना सीखने के बाद अगला जरूरी step है directories को संभालना। Real projects में data अलग-अलग folders में रखा जाता है, जैसे notes, images, reports और backup। इन folders को हर बार हाथ से बनाने या देखने की जगह आप Python code से यह काम कर सकते हैं।

इस tutorial में आप सीखेंगे कि directory क्या होती है और os.getcwd(), os.listdir(), os.mkdir(), os.makedirs(), os.path.exists(), os.path.isdir(), os.path.isfile(), os.chdir(), os.rename(), os.rmdir() और os.scandir() कैसे इस्तेमाल होते हैं। हर example के साथ possible Terminal output और VS Code Explorer में दिखने वाला folder structure भी दिया गया है।

यह lesson Python os Module in Hindi का directory-focused practical version है। अगर आपने file handling की basics नहीं पढ़ीं, तो पहले Python File Handling in Hindi: Core Concepts & Workflow और Python File Writing in Hindi देखें।

📌 इस Lesson का Scope: इस lesson में केवल os module से directory operations पर focus है। pathlib, shutil, os.walk(), file permissions और recursive folder deletion को detail में नहीं पढ़ाया गया है।
🎥 Video में Topic समझें

Directory क्या होती है?

Directory (आम भाषा में folder) एक ऐसी जगह है जिसमें files और दूसरी directories रखी जा सकती हैं। किसी directory के अंदर बनी directory को subdirectory या subfolder कहते हैं।

📁 VS Code Explorer में Folder Structure
PythonProject/ ├── main.py ├── notes.txt └── practice/ ├── day1.py └── day2.py

ऊपर PythonProject एक directory है। उसके अंदर दो files (main.py, notes.txt) और एक subdirectory (practice) है, जिसमें आगे दो files हैं।

Directory Operations की जरूरत क्यों पड़ती है?

  • Project के लिए folder structure अपने-आप तैयार करना।
  • किसी folder की files और subfolders की list देखना।
  • File और folder को अलग-अलग पहचानना।
  • Program की working location बदलना।
  • Folder का नाम बदलना।
  • खाली folder हटाना।
  • Reports, backups और downloads जैसे folders को organize रखना।
Safety Tip: os.rename() और os.rmdir() वाले examples पहले अपने practice folder में चलाएँ। किसी जरूरी folder का path इन functions को न दें।

os.getcwd() — Current Working Directory

os.getcwd() बताता है कि Python program इस समय किस directory में काम कर रहा है। Relative paths, जैसे "notes.txt", इसी directory के आधार पर resolve होते हैं।

Example
import os print("Current Directory:", os.getcwd())

Terminal Output (Possible):

Current Directory: C:\Users\YourName\Documents\PythonProject

आपके computer पर path अलग होगा।

VS Code में program चलाने पर working directory उस folder पर निर्भर करती है जिसे आपने VS Code में open किया है या जहाँ से Terminal में command चलाई है। अगर file नहीं मिल रही, तो पहले os.getcwd() print करके देखें।

os.listdir() — Folder Contents देखना

os.listdir() किसी directory में मौजूद files और folders के नामों की list देता है। Path न दें, तो यह current working directory की list देता है।

Example
import os items = os.listdir() print(items) for item in sorted(items): print(item)

Terminal Output (Possible):

['main.py', 'notes.txt', 'practice'] main.py notes.txt practice

Output आपके folder की वास्तविक content पर निर्भर करेगा।

ध्यान दें: os.listdir() की list का क्रम guaranteed नहीं होता। क्रम चाहिए तो sorted() इस्तेमाल करें। यह function सिर्फ नाम देता है, इसलिए नाम देखकर आप नहीं बता सकते कि entry file है या folder।

os.mkdir() — नया Folder बनाना

os.mkdir() एक नई directory बनाता है। इसकी parent directory पहले से मौजूद होनी चाहिए। उसी नाम की directory पहले से हो, तो FileExistsError आ सकती है।

Example
import os folder = "PythonPractice" if not os.path.exists(folder): os.mkdir(folder) print("Folder created:", folder) else: print("Folder already exists:", folder)

Terminal Output (पहली बार):

Folder created: PythonPractice

Terminal Output (दोबारा run करने पर):

Folder already exists: PythonPractice
📁 VS Code Explorer में अब ऐसा दिखेगा
PythonProject/ ├── main.py └── PythonPractice/

os.makedirs() — Nested Directories बनाना

कई levels वाले folders एक साथ बनाने हों, तो os.makedirs() इस्तेमाल करें। जरूरत पड़ने पर यह बीच के parent folders भी बना सकता है।

Example
import os os.makedirs("Projects/Python/Files", exist_ok=True) print("Nested directories are ready!")

Terminal Output:

Nested directories are ready!

exist_ok=True होने पर target directory पहले से मौजूद होने पर केवल उसी कारण से error नहीं आती, इसलिए यह code दोबारा चलाया जा सकता है।

📁 VS Code Explorer Structure
Projects/ └── Python/ └── Files/
Functionकब इस्तेमाल करें
os.mkdir()सिर्फ एक folder बनाना हो और parent पहले से मौजूद हो।
os.makedirs()Nested folders एक साथ बनाने हों।

os.path.exists() — Path Check करना

os.path.exists(path) बताता है कि दिए गए path पर कुछ मौजूद है या नहीं। वह file हो या directory, दोनों के लिए True मिलता है।

Example
import os path = "Projects" if os.path.exists(path): print("Path found") else: print("Path not found")

os.path.isdir() और os.path.isfile()

exists() सिर्फ इतना बताता है कि कुछ मौजूद है। यह जानने के लिए कि वह folder है या file, इन दोनों functions का इस्तेमाल होता है:

  • os.path.isdir(path) — path मौजूद directory है या नहीं।
  • os.path.isfile(path) — path मौजूद regular file है या नहीं।
Example
import os for name in ["Projects", "main.py", "missing_item"]: if os.path.isdir(name): print(name, "-> Directory") elif os.path.isfile(name): print(name, "-> File") else: print(name, "-> Not found")

Terminal Output (Possible):

Projects -> Directory main.py -> File missing_item -> Not found

os.chdir() — Current Directory बदलना

os.chdir() program की current working directory बदलता है। इसके बाद के सभी relative paths नई directory से resolve होते हैं। इसलिए बदलने से पहले पुरानी directory को variable में सुरक्षित रखना अच्छा तरीका है।

Example
import os os.makedirs("Projects", exist_ok=True) original = os.getcwd() print("Before:", original) os.chdir("Projects") print("After:", os.getcwd()) # वापस पुरानी directory में जाना os.chdir(original) print("Back:", os.getcwd())

Terminal Output (Possible):

Before: C:\Users\YourName\Documents\PythonProject After: C:\Users\YourName\Documents\PythonProject\Projects Back: C:\Users\YourName\Documents\PythonProject
Directory मौजूद न हो, तो os.chdir() से FileNotFoundError आ सकती है। Chdir के बाद अगर notes.txt जैसी file नहीं मिल रही, तो संभव है आप गलत working directory में हैं।

os.rename() — Directory Rename करना

os.rename(old, new) सिर्फ files के लिए नहीं, directories के लिए भी काम करता है। पुरानी directory मौजूद होनी चाहिए।

Example
import os old_name = "PythonPractice" new_name = "PythonLearning" if os.path.isdir(old_name) and not os.path.exists(new_name): os.rename(old_name, new_name) print("Directory renamed!") else: print("Rename नहीं हो सका: old folder नहीं मिला या new name पहले से मौजूद है")

Terminal Output (Possible):

Directory renamed!

यहाँ code पहले दोनों चीजें check करता है, यानी पुरानी directory मौजूद है और नया नाम पहले से इस्तेमाल में नहीं है। इससे गलती से किसी जरूरी चीज से टकराव का खतरा कम होता है।

os.rmdir() — Empty Directory Delete करना

os.rmdir() सिर्फ खाली directory हटाता है। अगर directory में कोई file या subfolder है, तो operation fail होता है और OSError आ सकती है।

Example
import os folder = "EmptyFolder" os.makedirs(folder, exist_ok=True) if os.path.isdir(folder) and not os.listdir(folder): os.rmdir(folder) print("Empty directory deleted") else: print("Directory missing or not empty")

Terminal Output:

Empty directory deleted
⚠️ Delete से पहले जाँचें

इस lesson में सिर्फ खाली directory हटाने का तरीका दिखाया गया है। किसी बड़े folder को recursively हटाने के लिए अलग tools और अतिरिक्त सावधानी की जरूरत होती है। जरूरी folders पर कभी बिना जाँच के delete न चलाएँ।

os.scandir() — Directory Entries देखना

os.scandir() किसी directory की entries को DirEntry objects के रूप में देता है। हर entry पर name, path, is_file() और is_dir() जैसी जानकारी मिलती है। जब आपको file और folder को अलग-अलग पहचानना हो, तो यह os.listdir() से ज्यादा सुविधाजनक है।

Example
import os with os.scandir(".") as entries: for entry in sorted(entries, key=lambda e: e.name): if entry.is_dir(): print("📁 Folder:", entry.name) elif entry.is_file(): print("📄 File:", entry.name)

Terminal Output (Possible):

📄 File: main.py 📄 File: notes.txt 📁 Folder: practice

यहाँ with statement का इस्तेमाल इसलिए हुआ है कि scandir() का iterator काम खत्म होने पर ठीक से बंद हो जाए। sorted(..., key=lambda e: e.name) entries को नाम के अनुसार क्रम में रखता है, क्योंकि scandir() भी किसी निश्चित क्रम की गारंटी नहीं देता।

Featureos.listdir()os.scandir()
क्या मिलता हैनामों की listDirEntry objects
File/folder पहचानअलग से os.path.isdir() चाहिएentry.is_dir(), entry.is_file()
Full pathos.path.join() से बनाना पड़ता हैentry.path
कब अच्छा हैसिर्फ नामों की जरूरत होनाम के साथ type भी चाहिए

Practical Example — Folder Structure Setup

अब पिछले functions को मिलाकर एक छोटा program बनाते हैं। यह PythonProject के अंदर notes, images और reports folders बनाएगा और फिर उनकी जानकारी print करेगा।

Example — Project Folders बनाना और जाँचना
import os base = "PythonProject" folders = ["notes", "images", "reports"] # 1. Base folder बनाना os.makedirs(base, exist_ok=True) # 2. Subfolders बनाना for name in folders: path = os.path.join(base, name) os.makedirs(path, exist_ok=True) print("Ready:", path) # 3. जाँच करना print("\nVerification:") for name in sorted(os.listdir(base)): full_path = os.path.join(base, name) if os.path.isdir(full_path): print(name, "-> Directory")

Terminal Output (Possible):

Ready: PythonProject/notes Ready: PythonProject/images Ready: PythonProject/reports Verification: images -> Directory notes -> Directory reports -> Directory

Windows पर path में backslash दिख सकता है।

📁 VS Code Explorer में verify करें
PythonProject/ ├── images/ ├── notes/ └── reports/

Mini Project — Workspace Directory Report

अब एक mini project बनाते हैं। यह program MyWorkspace नाम का workspace बनाएगा, उसमें चार folders तैयार करेगा, दो sample files लिखेगा और फिर हर folder में कितनी entries हैं, इसकी report दिखाएगा। इसमें makedirs(), os.path.join(), open(), scandir(), listdir() और is_dir() सभी का इस्तेमाल है।

Mini Project — workspace_report.py
import os base = "MyWorkspace" folders = ["notes", "images", "reports", "backup"] # 1. Workspace और subfolders बनाना os.makedirs(base, exist_ok=True) for name in folders: os.makedirs(os.path.join(base, name), exist_ok=True) # 2. Sample files लिखना samples = { "notes": "python.txt", "reports": "report.txt", } for folder, file_name in samples.items(): file_path = os.path.join(base, folder, file_name) with open(file_path, "w", encoding="utf-8") as file: file.write(f"Sample file in {folder} folder\n") # 3. Directory report बनाना total_dirs = 0 total_items = 0 print("=== Workspace Report ===") with os.scandir(base) as entries: for entry in sorted(entries, key=lambda e: e.name): if entry.is_dir(): total_dirs += 1 count = len(os.listdir(entry.path)) total_items += count print(f"{entry.name:<10 -=""> {count} item(s)") print("------------------------") print("Folders:", total_dirs) print("Total files inside folders:", total_items)

Terminal Output (Possible):

=== Workspace Report === backup -> 0 item(s) images -> 0 item(s) notes -> 1 item(s) reports -> 1 item(s) ------------------------ Folders: 4 Total files inside folders: 2
📁 VS Code Explorer में Final Structure
MyWorkspace/ ├── backup/ ├── images/ ├── notes/ │ └── python.txt └── reports/ └── report.txt

यह program दोबारा चलाने पर भी error नहीं देगा, क्योंकि folders exist_ok=True से बन रहे हैं। हाँ, sample files फिर से लिखी जाएँगी, यानी उनकी पुरानी content replace हो जाएगी। इसलिए इसे सिर्फ practice workspace में चलाएँ।

Common Mistakes

❌ 1. मौजूद folder को फिर से os.mkdir() से बनाना
os.mkdir("PythonPractice")

Folder पहले से मौजूद हो, तो FileExistsError आ सकती है।

✅ सही तरीका
os.makedirs("PythonPractice", exist_ok=True)
❌ 2. Parent folder के बिना nested folder बनाने के लिए os.mkdir() इस्तेमाल करना
os.mkdir("Projects/Python/Files")

अगर Projects या Projects/Python मौजूद नहीं है, तो FileNotFoundError आ सकती है।

✅ सही तरीका
os.makedirs("Projects/Python/Files", exist_ok=True)
❌ 3. os.listdir() के नाम को देखकर file या folder मान लेना

सिर्फ नाम से पता नहीं चलता कि entry file है या folder। Folder का नाम भी data.v1 जैसा हो सकता है।

✅ सही तरीका
for name in os.listdir("."): if os.path.isdir(name): print("Folder:", name)

या फिर os.scandir() और entry.is_dir() इस्तेमाल करें।

❌ 4. Non-empty directory को os.rmdir() से हटाना

os.rmdir() सिर्फ खाली directory हटाता है। Directory में content हो, तो OSError आ सकती है।

✅ सही तरीका
if os.path.isdir("EmptyFolder") and not os.listdir("EmptyFolder"): os.rmdir("EmptyFolder")
❌ 5. os.chdir() के बाद पुरानी directory भूल जाना

os.chdir() पूरे program की working directory बदल देता है। उसके बाद relative paths अलग जगह से resolve होते हैं और files मिलना बंद हो सकती हैं।

✅ सही तरीका
original = os.getcwd() os.chdir("Projects") # काम पूरा करें os.chdir(original)
❌ 6. Rename में नया नाम पहले से मौजूद होना

Destination path पहले से मौजूद हो, तो behavior operating system के अनुसार अलग हो सकता है और error आ सकती है।

✅ सही तरीका
if os.path.isdir("old_folder") and not os.path.exists("new_folder"): os.rename("old_folder", "new_folder")

Quick Exercise 🧠

Nested directories (जैसे Projects/Python/Files) एक साथ बनाने के लिए कौन सा function सही है?

A) os.mkdir()
B) os.rmdir()
C) os.makedirs()

सही उत्तर: C — os.makedirs()

Final Coding Challenge

अब VS Code में directory_practice.py नाम की file बनाएँ और नीचे दिए गए tasks पूरा करें। Rename और delete वाले tasks के लिए केवल अपनी बनाई हुई practice directories का इस्तेमाल करें।

  1. os.getcwd() से current directory print करें।
  2. os.mkdir() से DirPractice बनाएँ; पहले existence check जरूर करें।
  3. os.makedirs() से DirPractice/Level1/Level2 बनाएँ।
  4. os.path.exists() और os.path.isdir() से दोनों folders check करें।
  5. एक text file DirPractice/info.txt बनाएँ और os.path.isfile() से उसे check करें।
  6. os.listdir() से DirPractice की entries print करें।
  7. os.scandir() से हर entry को "Folder" या "File" label के साथ print करें।
  8. os.chdir() से DirPractice में जाएँ, os.getcwd() print करें और फिर पुरानी directory में वापस आएँ।
  9. Level2 को os.rename() से Level2_Renamed करें।
  10. Level2_Renamed खाली है, इसलिए उसे os.rmdir() से हटाएँ।
  11. Bonus: VS Code Explorer खोलकर हर step के बाद अपना folder structure verify करें।

Quick Revision

Functionकाम
os.getcwd()Current working directory बताता है।
os.listdir(path)Directory की entries के नाम list में देता है।
os.mkdir(path)एक directory बनाता है।
os.makedirs(path, exist_ok=True)Nested directories बना सकता है।
os.path.exists(path)Path मौजूद है या नहीं check करता है।
os.path.isdir(path)Path directory है या नहीं check करता है।
os.path.isfile(path)Path regular file है या नहीं check करता है।
os.chdir(path)Current working directory बदलता है।
os.rename(old, new)Directory या file rename करता है।
os.rmdir(path)खाली directory delete करता है।
os.scandir(path)Directory entries को DirEntry objects के रूप में देता है।

FAQ — Python Directory Operations

1. Python में directory क्या होती है?

Directory को आम भाषा में folder कहते हैं। यह एक container है जिसमें files और दूसरी directories (subfolders) रखी जा सकती हैं। Python में os module से directories बनाई, देखी, बदली, rename और delete की जा सकती हैं।

2. Directory और folder में क्या फर्क है?

Programming और file system की भाषा में इन्हें आमतौर पर एक ही चीज़ माना जाता है। Windows और VS Code जैसे tools में इसे folder कहा जाता है, जबकि technical documentation में अक्सर directory शब्द इस्तेमाल होता है।

3. os.getcwd() क्या करता है?

os.getcwd() current working directory का path string के रूप में देता है। यह वही directory होती है जिसे Python program relative paths के लिए आधार मानता है।

4. os.listdir() और os.scandir() में क्या अंतर है?

os.listdir() entries के नामों की list देता है, जबकि os.scandir() हर entry के लिए DirEntry object देता है, जिस पर name, path, is_file() और is_dir() जैसे attributes और methods मिलते हैं। File और folder को अलग करना हो, तो os.scandir() ज्यादा सुविधाजनक है।

5. os.mkdir() और os.makedirs() में क्या अंतर है?

os.mkdir() एक directory बनाता है और उसकी parent directory पहले से मौजूद होनी चाहिए। os.makedirs() जरूरत पड़ने पर बीच की सभी parent directories भी बना सकता है।

6. os.makedirs() में exist_ok=True का क्या मतलब है?

exist_ok=True देने पर अगर target directory पहले से मौजूद है, तो सिर्फ इसी कारण FileExistsError नहीं आती। इससे code दोबारा चलाना आसान हो जाता है।

7. Directory मौजूद है या नहीं, कैसे check करें?

os.path.exists(path) से path मौजूद होने की जाँच होती है और os.path.isdir(path) से यह पता चलता है कि वह path directory है या नहीं।

8. os.chdir() से directory बदलने के बाद क्या ध्यान रखना चाहिए?

os.chdir() पूरे program की current working directory बदल देता है, इसलिए उसके बाद के सभी relative paths उसी नई directory से resolve होंगे। बदलने से पहले os.getcwd() से पुरानी directory सुरक्षित रख लेना अच्छा तरीका है।

9. os.rmdir() non-empty directory क्यों delete नहीं करता?

os.rmdir() केवल खाली directory हटाने के लिए बना है। Directory में files या subfolders हों, तो OSError आ सकती है। यह एक तरह की सुरक्षा भी है, ताकि गलती से पूरा folder tree न हट जाए।

10. Directory rename करने के लिए कौन सा function इस्तेमाल होता है?

os.rename(old_name, new_name) से directory का नाम बदला जा सकता है। पुरानी directory मौजूद होनी चाहिए और नए नाम का path पहले से किसी जरूरी चीज़ से टकराना नहीं चाहिए।

Summary

Python Directory Operations = os module से folders बनाना, देखना, बदलना, rename करना और खाली folders हटाना।

इस lesson में आपने directory का मतलब, os.getcwd(), os.listdir(), os.mkdir(), os.makedirs(), os.path.exists(), os.path.isdir() और os.path.isfile(), os.chdir(), os.rename(), os.rmdir() और os.scandir() सीखे, साथ ही एक mini project भी बनाया।

अब Final Coding Challenge पूरा करें और हर step के बाद VS Code Explorer में अपना folder structure verify करें। जब यह comfortable लगे, तो modern तरीके के लिए Python pathlib Module in Hindi lesson पढ़ें।

Related Articles — Python Hindii

🧑‍💻 Python Coding Lab

Python code likho, run karo aur practice karo — directly browser mein.